/*
 * ImageResults.java
 * 
 * Created on Jul 31, 2007, 1:28:21 PM
 * 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.mbari.dsg;

import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import org.mbari.vars.knowledgebase.model.Concept;
import org.mbari.vars.knowledgebase.model.Media;
import org.mbari.vars.knowledgebase.model.dao.ConceptDAO;
/**
 *
 * @author brian
 */
public class ImageResults {

    public ImageResults() {
    }
    
    public static String generateHtmlStub(String conceptName) {
        StringBuilder builder = new StringBuilder();
        try {
            Concept concept = ConceptDAO.getInstance().findByName(conceptName);
            generateDiv(concept, builder, 1);
        } 
        catch (Exception e) {
            builder.append("ERROR: " + e.getMessage() + ". Failed to look up " + conceptName);
        }
        
        return builder.toString();
    }
    
    
    private static void generateDiv(Concept concept, StringBuilder builder, int n) {
        
        // <div id="C1" class="C-subclass">
        builder.append("<div ").append("\" class=\"conceptdiv").append(n).append("\">");
        builder.append("<span class=\"concept\">");
        builder.append("<div class=\"conceptname\"><h3 class=\"conceptname2\">");
        builder.append(concept.getPrimaryConceptNameAsString()).append(" [");
        String rankLevel = concept.getRankLevel();
        if (rankLevel != null) {
            builder.append(rankLevel);
        }
        String rankName = concept.getRankName();
        if (rankName != null) {
            builder.append(rankName);
        }
       builder.append("]</h3></div>\n");
        
        Set mediaSet = concept.getMediaSet();
        for (Iterator i = mediaSet.iterator(); i.hasNext();) {
            Media media = (Media) i.next();
            if (media.getType().equals(Media.TYPE_IMAGE)) {
                builder.append("<div class=\"image\"><span class=\"photo\">");
                builder.append("<a href=\"").append(media.getUrl()).append("\">");
                builder.append("<img src=\"").append(media.getUrl()).append("\" width=\"200\"/></a>");
                builder.append("</span></div>\n");
            }
        }
       
        Collection children = concept.getChildConceptColl();
        n = (n == 1) ? 2 : 1;
        for (Iterator i = children.iterator(); i.hasNext();) {
            Concept concept1 = (Concept) i.next();
            generateDiv(concept1, builder, n);
        }
        builder.append("</span></div>\n");

    }

}
